import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'
import Users from './components/user/Users.vue'
import Rights from './components/rights/Rights.vue'
import Roles from './components/rights/Roles.vue'
import Categories from './components/goods/Categories.vue'
import Params from './components/goods/Params.vue'
import GoodsList from './components/goods/List.vue'
import addGoods from './components/goods/add.vue'
import Order from './components/Order/Orders.vue'
import Report from './components/report/Report.vue'
Vue.use(Router)

const router = new Router({

    routes: [
        { path: '/login', component: Login },
        { path: '/', redirect: '/login' },
        {
            path: '/home',
            redirect: '/welcome',
            component: Home,
            children: [{ path: '/welcome', component: Welcome }, { path: '/users', component: Users }, { path: '/rights', component: Rights },
                { path: '/roles', component: Roles }, { path: '/categories', component: Categories }, { path: '/Params', component: Params },
                { path: '/goods', component: GoodsList, children: [] }, { path: '/goods/add', component: addGoods }, { path: '/orders', component: Order },
                { path: '/reports', component: Report }
            ]
        },

    ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next() //如果是login路由就放行
    if (!window.sessionStorage.getItem('token')) {
        return next('/login')
    } else next() //放行
})
export default router